package com.ruoyi.web.controller.note;

import java.util.List;
import javax.servlet.http.HttpServletResponse;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.domain.Ztree;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.note.domain.NoteCategory;
import com.ruoyi.note.service.INoteCategoryService;

/**
 * 笔记分类Controller
 * 
 * @author ruoyi
 * @date 2025-01-22
 */
@RestController
@RequestMapping("/note/category")
public class NoteCategoryController extends BaseController
{
    @Autowired
    private INoteCategoryService noteCategoryService;

    /**
     * 查询笔记分类列表
     */
    @PreAuthorize("@ss.hasPermi('note:category:list')")
    @GetMapping("/list")
    public AjaxResult list(NoteCategory noteCategory)
    {
        List<NoteCategory> list = noteCategoryService.selectNoteCategoryList(noteCategory);
        return success(list);
    }

    /**
     * 查询笔记分类树结构
     */
    @PreAuthorize("@ss.hasPermi('note:category:list')")
    @GetMapping("/treeselect")
    public AjaxResult treeselect(NoteCategory noteCategory)
    {
        List<NoteCategory> categories = noteCategoryService.selectNoteCategoryList(noteCategory);
        return success(noteCategoryService.buildCategoryTree(categories));
    }

    /**
     * 加载对应角色分类列表树
     */
    @GetMapping(value = "/categoryTreeData")
    public AjaxResult categoryTreeData()
    {
        List<Ztree> ztrees = noteCategoryService.initZtree(noteCategoryService.selectNoteCategoryList(new NoteCategory()));
        return success(ztrees);
    }

    /**
     * 导出笔记分类列表
     */
    @PreAuthorize("@ss.hasPermi('note:category:export')")
    @Log(title = "笔记分类", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, NoteCategory noteCategory)
    {
        List<NoteCategory> list = noteCategoryService.selectNoteCategoryList(noteCategory);
        ExcelUtil<NoteCategory> util = new ExcelUtil<NoteCategory>(NoteCategory.class);
        util.exportExcel(response, list, "笔记分类数据");
    }

    /**
     * 获取笔记分类详细信息
     */
    @PreAuthorize("@ss.hasPermi('note:category:query')")
    @GetMapping(value = "/{categoryId}")
    public AjaxResult getInfo(@PathVariable("categoryId") Long categoryId)
    {
        return success(noteCategoryService.selectNoteCategoryByCategoryId(categoryId));
    }

    /**
     * 新增笔记分类
     */
    @PreAuthorize("@ss.hasPermi('note:category:add')")
    @Log(title = "笔记分类", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody NoteCategory noteCategory)
    {
        if (!noteCategoryService.checkCategoryNameUnique(noteCategory))
        {
            return error("新增分类'" + noteCategory.getCategoryName() + "'失败，分类名称已存在");
        }
        noteCategory.setCreateBy(getUsername());
        return toAjax(noteCategoryService.insertNoteCategory(noteCategory));
    }

    /**
     * 修改笔记分类
     */
    @PreAuthorize("@ss.hasPermi('note:category:edit')")
    @Log(title = "笔记分类", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody NoteCategory noteCategory)
    {
        if (!noteCategoryService.checkCategoryNameUnique(noteCategory))
        {
            return error("修改分类'" + noteCategory.getCategoryName() + "'失败，分类名称已存在");
        }
        noteCategory.setUpdateBy(getUsername());
        return toAjax(noteCategoryService.updateNoteCategory(noteCategory));
    }

    /**
     * 删除笔记分类
     */
    @PreAuthorize("@ss.hasPermi('note:category:remove')")
    @Log(title = "笔记分类", businessType = BusinessType.DELETE)
    @DeleteMapping("/{categoryIds}")
    public AjaxResult remove(@PathVariable String categoryIds)
    {
        return toAjax(noteCategoryService.deleteNoteCategoryByCategoryIds(categoryIds));
    }
}